package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;

/**
 * Servlet implementation class DeleteEventServlet
 */
@WebServlet("/DeleteEventServlet")
public class DeleteEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");  
		PrintWriter out = response.getWriter();
		
		HttpSession session=request.getSession(true);
		 
		int idEvent=Integer.parseInt(request.getParameter("idEvent"));
		
		EventService es=new EventService();
		EventSignUpService esup=new EventSignUpService();
		
		try{
			esup.unregisterAllVisitorforEvent(idEvent);
				if(es.deleteEvent(idEvent)==1){
					ShowAllEventsServlet view=new ShowAllEventsServlet();
	    			view.doPost(request, response);
				}
				else{
					session.setAttribute("error", "Not deleted");
		    		 
		    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_showEvents.jsp");  
		    		rd.include(request,response);
				}
			
		}
		catch(Exception eee){
	    	eee.getMessage();	
	    }
	          
	    out.close();  
}
}
